<html xmlns:th="http://www.thymeleaf.org">
<html>
  <head>
    <base href="<%=basePath%>">
      <meta charset="UTF-8">
    <title>为用户分配角色</title>
	<meta name="pragma" content="no-cache">
	<meta name="cache-control" content="no-cache">
	<meta name="expires" content="0">
    <div th:replace="~{views/commons/script::script}"></div>

  </head>
  <body>
    <!-- 把所有的角色查询出来展示到这个位置 -->
    <input type="hidden" name="userId" value="${user.id }" >
    <table id="dataList" width="100%"></table>
    <div style="text-align: center;margin-top:10px;">
        <input type="hidden" th:value="${userId}" id="userid">
    	<a  class="easyui-linkbutton" id="assignBtn" data-options="iconCls:'icon-man'">分配</a>
    </div>
    <script type="text/javascript">
    	$(function(){
            // 为用户分配的角色
    		$("#assignBtn").on("click",function(){
    			//获取用户的id
    			var userId = $("#userid").val();
    			//获取到选中角色信息
    			var rows = $("#dataList").datagrid("getChecked");
    			if(!rows.length){
    				$.messager.alert("警告","请选中需要为用户分配的角色");
    				return;
    			}
                var roleId = rows[0].id;
    			
    			$.post("/system/user/UserAction_updateRoles.action",
    			{
    				"userId" : userId,
    				"ids" : roleId
    			},
    			function(data){
                    if(data.success==true){
                        $.messager.alert("提示",data.msg,"info",function (){
                            parent.closeTopWindow();
                        });

                    }else{
                        $.messager.alert("警告",data.msg,"info");
                    }
    			},"json");
    		});
    		//加载所有角色
    		$("#dataList").datagrid({
    			url : "/system/role/RoleAction_findAll",
                pagination : true,
                toolbar : "#toolbar",
                fitColumns : true,
                idField : "id",
                rownumbers : true,
    			columns : [
                    [
    				{field:"id",title:"选择",checkbox:true},
    				{field:"rolename",title:"角色名",sortable:true,width:10},
    				{field:"rolenote",title:"角色描述",width:10},
    				{field:"status",title:"角色状态",formatter:function(value,rowData,index){
    					if(value == 0){
    						return "可用";
    					}else if(value == 1){
    						return "禁用";
    					}else if(value == 2){
    						return "已删除";
    					}else{
    						return "";
    					}
    				}}
    			]
                ],
                loadFilter:function(data){
                    //data是服务器返回的数据,将服务器端返回的数据转换为datagrid需要的格式
                    return {"total":data.totalRows,"rows":data.result};
                },
    		});
    	});
    </script>
  </body>
</html>
